The pipe

GVPT399F: Power, Politics, and Data

Combine multiple operations with the pipe

That got messy!

  • We had a lot of different objects representing intermediate steps in our calculations.

  • We never need those objects again. Can we avoid creating them?

Let’s introduce perhaps the defining feature of the tidyverse: the pipe.

Combine multiple operations with the pipe

Read the pipe as:

Take this |> (and then…)
    do this |> (and then…)
    do this

gapminder |> 
  group_by(continent) |> 
  summarise(avg_pop = mean(pop), avg_gdp_per_cap = mean(gdpPercap)) |> 
  arrange(avg_gdp_per_cap)
# A tibble: 5 × 3
  continent   avg_pop avg_gdp_per_cap
  <fct>         <dbl>           <dbl>
1 Africa     9916003.           2194.
2 Americas  24504795.           7136.
3 Asia      77038722.           7902.
4 Europe    17169765.          14469.
5 Oceania    8874672.          18622.

Combine multiple operations with the pipe

gapminder |> 
  group_by(continent) |> 
  summarise(avg_pop = mean(pop), avg_gdp_per_cap = mean(gdpPercap)) |> 
  ggplot(aes(x = continent, y = avg_gdp_per_cap)) + 
  geom_col() + 
  theme_minimal()

A note on the pipe

Base pipe:

  • |>

  • Can be used without loading any packages

  • Relatively new: introduced in 2021

Tidyverse pipe:

  • %>%

  • Must load dplyr or magrittr to use

Summary

This session you have:

  1. Learnt R basic syntax

  2. Learnt how to transform your data

  3. Written concise code that is easy to follow